import sys
import string
import re

def getIOPTPattern():
    return "^search in (.*): .*"

def getArcPattern():
    return "^arc [0-9]+ ===> state [0-9]+ .*"

def getStatePattern():
    return "^state ([0-9]+), .*: (.*)"

def getTransitionPattern():
    return "^arc [0-9]+ ===> state ([0-9]+) \(c?rl .* => .* \[.*label ?(.*)\] .\)"

def hasState(line):
    return re.match(getStatePattern(), line)

def hasTrasition(line):
    return re.match(getTransitionPattern(), line)
    
def hasArc(line):
    return re.match(getArcPattern(), line)

def getIOPTName(line):
    name = re.match(getIOPTPattern(), line)
    if name:
        return "digraph " + name.group(1) + " {\n\n"
    return ""

def BEGIN(line):
    return getIOPTName(line)

def getLine(lines, index):
    line = lines[index].strip() + " "
    if hasState(lines[index]) or hasArc(lines[index]):
        while index < len(lines) - 1 and not (hasArc(lines[index+1]) or hasState(lines[index+1]) or lines[index+1].strip() == "Bye."):
            index = incrementIndex()
            line += lines[index].strip() + " "
    return line.strip()

def incrementIndex():
    global index
    index += 1
    return index

def setCurrentState(new_state):
    global current_state
    current_state = new_state

def getCurrentState():
    global current_state
    return current_state

def replace(string, keys):
    
    for key in keys:
        string = string.replace(key, "")
    
    chars = [",", "(", ")"]
    for c in chars:
        string = string.replace(c, " ")
    
    return [string.strip(), "idle"][len(string.strip()) == 0]

def getString(delimiters, string, null):
    return delimiters[0] + " ".join(replace(string, null).strip().split()) + delimiters[1]

def getState(line):
    result = re.match(getStatePattern(), line)
    if result:
        setCurrentState(result.group(1))
        return "  s" + getCurrentState() + " [label=\"" + getString("()", result.group(2), "empty") + "\"]\n"
    return ""

def getTransition(line):
    result = re.match(getTransitionPattern(), line)
    if result:
        return "  s" + getCurrentState() + " -> " + "s" + result.group(1) + " [label=\"" + result.group(2) + "\"]\n"
    return ""

def END():
    return "}"

lines = sys.stdin.readlines()
index = 0
exit = []
current_state = ""
dot = ""

while index < len(lines):
    line = getLine(lines, index)
    exit.append(line)
    incrementIndex()

for i in exit:
    dot += getIOPTName(i)
    dot += getState(i)
    dot += getTransition(i)
    
dot += END()
print dot
